import { payWxPay, queryWxPayStatus } from "@/api/wx";
import { useImStore } from "@/store"
export default class WxPay {
	constructor() { }

	imStore = useImStore();
	async pay(options, funs) {
		console.log(options);
		payWxPay({
			order_id: options.orderId,
			type: options.type,
			source: options.source,
		}).then((res : any) => {
			uni.requestPayment({
				provider: 'wxpay',
				...res,
				success: () => {
					if (funs && funs.success) {
						funs.success(options.orderId)
					} else {
						this.getOrder(options.orderId, options.type, funs.getOrderCb)
					}

				},
				fail: (err) => {
					if (funs && funs.fail) {
						funs.fail(err)
					} else {
						this.imStore.$patch({
							doImIng: false,
						});
						uni.$wdToast.error("取消支付")
					}

				}
			})
			console.log(res);
		})
	}

	getOrder(orderId, type, fun) {
		// fun({})
		queryWxPayStatus({ order_id: orderId, type: type }).then((res : any) => {
			if (res.is_pay === 1) {
				fun(res)
			} else {
				this.getOrder(orderId, type, fun);
			}
		})
	}
}